package cn.wgx.module.mind.service;

import cn.wgx.common.entity.mind.ProjectRole;
import cn.wgx.common.entity.mind.ProjectUser;
import cn.wgx.common.util.UUIDUtils;
import cn.wgx.module.mind.entity.mapper.ProjectRoleMapper;
import cn.wgx.module.mind.util.ProjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.NoPermissionException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class ProjectRoleService extends BaseMindService<ProjectRoleMapper, ProjectRole> {

    @Autowired
    ProjectUserService projectUserService;


    @Transactional
    public boolean save(ProjectRole projectRole) {
        //检测修改权限
        if(!ProjectUtil.isCreateUser(projectRole.getPro_id())){
            return false;
        }
        ini(projectRole);
        return super.save(projectRole);
    }
    @Transactional
    public boolean saveBatch(Collection<ProjectRole> projectRole){
        for(ProjectRole p: projectRole){
            if(!ProjectUtil.isCreateUser(p.getPro_id())){
                return false;
            }
            ini(p);
        }
        return super.saveBatch(projectRole);
    }
    @Transactional
    public boolean updateById(ProjectRole entity) {
        entity.setEName(null);
        return super.updateById(entity);
    }

    @Transactional
    public boolean removeById(Integer id){
        return canDel(id) && super.removeById(id);
    }

    @Transactional
    public boolean removeByIds(List<Integer> ids){
        List<Integer> delIds = new ArrayList<>();
        for(Integer id: ids){
            if(canDel(id)){
                delIds.add(id);
            }
        }
        return super.removeByIds(delIds);
    }

    //检测改权限
    public boolean checkModifyP(Integer pro_id, Integer user_id){
        if(pro_id == null || user_id == null){
            return false;
        }
        QueryWrapper<ProjectRole> queryWrapper = new QueryWrapper();
        queryWrapper.eq("p_modify", 1);
        queryWrapper.inSql("pr_id",
                "(select pro_role_id from mind_pro_user where user_id="+user_id+" and pro_id = "+pro_id+") ");
        queryWrapper.or(i -> i.eq("createUserId", user_id));
        queryWrapper.eq("pro_id",pro_id);
        Integer integer = baseMapper.selectCount(queryWrapper);
        if(integer > 0){
            return true;
        }
        return false;
    }

    //检测是否可删
    private boolean canDel(Integer id){
        QueryWrapper<ProjectUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("pro_role_id",id);
        Page<ProjectUser> page = projectUserService.page(new Page<>(1, 1), queryWrapper);
        if(!page.getRecords().isEmpty()){
            return false;
        }
        ProjectRole projectRole = baseMapper.selectById(id);
        if(projectRole != null){
            return !projectRole.getRoleName().equals("admin");
        }
        return false;
    }

    private void ini(ProjectRole projectRole){
        projectRole.setEName(UUIDUtils.getGUID());
    }
}
